package com.coder520.login.controller;

import com.coder520.commom.utils.sercutyUtils.SercutyUtil;
import com.coder520.user.entity.User;
import com.coder520.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by PC on 2017/7/29.
 */

@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * @Author jinpengshen [www.coder520.com]
     * @Date 2017/7/30 21:29
     * @Description 登陆页面
     */
    @RequestMapping("/index")
    public String login(){

        return "login";
    }

    /**
     * @Author jinpengshen [www.coder520.com]
     * @Date 2017/7/30 21:30
     * @Description 校验登陆
     */
    @ResponseBody
    @RequestMapping("/check")
    public String checkLogin(HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        String username = request.getParameter("username");
        String passwrod = request.getParameter("password");
        User user = userService.findUserByName(username);
        if(user != null){
            if(SercutyUtil.checkPwd(passwrod,user.getPassword())){
                request.getSession().setAttribute("userinfo",user);
                return "login_succ";
            }else{
                return "login_fail";
            }
        }else{
            return "login_fail";
        }
    }
    /**
     * @Author jinpengshen [www.coder520.com]
     * @Date 2017/7/30 22:19
     * @Description 插入测试数据
     */
    @RequestMapping("/regis")
    public String creatUser() throws UnsupportedEncodingException, NoSuchAlgorithmException {

        User user = new User();
        user.setUsername("laowang");
        user.setRealName("laowang");
        user.setPassword(SercutyUtil.encreptyPassword("123456"));
        user.setMobile("192391239123");
        user.setHeadImage("asdwasdasdw.jpg");
        user.setId(1L);
        userService.creatUser(user);
        return "";
    }
}
